import 'package:aqua_plunge/model/Author.dart';
import 'package:aqua_plunge/model/Creation.dart';
import 'package:aqua_plunge/model/common.dart';
import 'package:aqua_plunge/widget/custom_avatar.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../controller/DatabaseListController.dart';
import '../services/database_service.dart';

class BroadCasterItemWidget extends StatefulWidget {

  final Creation creation;
  const BroadCasterItemWidget({super.key, required this.creation});

  @override
  State<BroadCasterItemWidget> createState() => _BroadCasterItemWidgetState();
}

class _BroadCasterItemWidgetState extends State<BroadCasterItemWidget> with AutomaticKeepAliveClientMixin{
  List<Common> commonList = [];
  final DatabaseService _databaseService = DatabaseService.instance;
  final DatabaseController _databaseController = Get.find<DatabaseController>();

  @override
  void initState() {
    super.initState();
    _databaseController.getAuthorByUserId(widget.creation.userId.toString());
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Obx(() {
      Author? author = _databaseController.authors[widget.creation.userId.toString()];

      return Container(
        width: double.infinity,
        decoration: BoxDecoration(borderRadius: BorderRadius.circular(12)),
        child: PhysicalModel(
          color: Colors.transparent,
          clipBehavior: Clip.antiAlias,
          borderRadius: BorderRadius.circular(14),
          child: Stack(
            children: [
              AspectRatio(aspectRatio:_databaseController.imageRatios[widget.creation.postId!]??0.7,
                child:
                Image.asset(
                widget.creation.cover ?? "", fit: BoxFit.cover,
                width: double.infinity)
              ),
              Positioned(
                  top: 12,
                  right: 10,
                  child: GestureDetector(child:
                  widget.creation.like != null?
                  Image.asset(
                    widget.creation.like == 1?
                    "images/icon_liked.png":"images/icon_unlike.png",
                    width: 24,
                    height: 24,
                  ):const SizedBox.shrink(),onTap: (){
                    _databaseService.updateCreationLike(
                        widget.creation.postId!,
                        widget.creation.like==1?0:1);
                    _databaseController.fetchFavoriteData();
                    _databaseController.fetchCreationData();
                    setState(() {
                      widget.creation.like = widget.creation.like == 1?0:1;
                    });
                  },)),
              Positioned(
                bottom: 0,
                left: 0,
                right: 0,
                child: Stack(
                  children: [
                    widget.creation.cover != null?
                    Image.asset(
                      "images/icon_bottom_tv_bg.png",
                      fit: BoxFit.fill,
                    ):const SizedBox.shrink(),
                    Container(
                      padding: const EdgeInsets.only(top: 4, left: 8),
                      child: Column(
                        children: [
                          SizedBox(
                            width: double.infinity,
                            child: Text(
                              widget.creation.title ?? "",
                              textAlign: TextAlign.left,
                              maxLines: 1,
                              style: const TextStyle(
                                  fontSize: 12, color: Colors.white),
                            ),
                          ),
                          const SizedBox(
                            height: 5,
                          ),
                          Row(
                            children: [
                              CustomAvatar(width: 24, height: 24, netPath: author?.avatar ?? ''),
                              const SizedBox(
                                width: 10,
                              ),
                              Expanded(
                                  child: Text(
                                    author?.nickname ?? "",
                                    style: const TextStyle(
                                        color: Colors.white,
                                        fontSize: 12,
                                        fontWeight: FontWeight.bold),
                                    maxLines: 1,
                                    overflow: TextOverflow.clip,
                                  )),
                              const SizedBox(width: 5)
                            ],
                          )
                        ],
                      ),
                    )
                  ],
                ),
              )
            ],
          ),
        ),
      );
    });
  }

  @override
  bool get wantKeepAlive => true;

}
